package com.example.recyclerimageload.glide_load_img.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.recyclerimageload.R;
import com.example.recyclerimageload.glide_load_img.bean.UrlsBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> implements View.OnClickListener, View.OnLongClickListener {
    private static String TAG = "cai";
    private static MyAdapter myAdapter;
    private List<UrlsBean> mDatas;
    private OnPositionListener positionListener;
    private OnItemClickListener itemClickListener;
    private MyHolder myHolder;
    private Context mContext;


    public static MyAdapter getInstance(){
        if (null == myAdapter){
            synchronized (MyAdapter.class){
                if (null==myAdapter){
                    myAdapter = new MyAdapter();
                }
            }
        }
        return myAdapter;
    }

    public void setDatas(List<UrlsBean> datas){
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        int viewType;
        if (mDatas.get(position).isFirst()){
            viewType = 0;
        } else {
            viewType = 1;
        }
        return viewType;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.mContext = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if (viewType == 0){
            view = inflater.inflate(R.layout.item_0, parent, false);
        } else {
            view = inflater.inflate(R.layout.item_1,parent,false);
        }
        myHolder = new MyHolder(view,viewType);
        return myHolder;
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        boolean first = mDatas.get(position).isFirst();
        positionListener.onPosition(position);
        MyHolder myHolder = (MyHolder) holder.itemView.getTag();;

        if (first){
            holder.text.setText(mDatas.get(position).getData());
        } else {
            if (myHolder == null){
                Log.d(TAG, "onBindViewHolder: myHolder == null  position="+position);
                myHolder = holder;
                holder.itemView.setTag(holder);
            } else {
                Log.d(TAG, "onBindViewHolder: myHolder != null position="+position);
                myHolder = (MyHolder) holder.itemView.getTag();
            }
            RequestOptions options = new RequestOptions();
            options.centerCrop();
            options.override(100,100);
            options.skipMemoryCache(true);
            options.diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(mContext)
                    .load(mDatas.get(position).getUrl())
                    .apply(options)
                    .into(myHolder.mImg);

            holder.itemView.setOnClickListener(this);
            holder.itemView.setOnLongClickListener(this);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onClick(View v) {
        itemClickListener.onItemClick(v);
    }

    @Override
    public boolean onLongClick(View v) {
        itemClickListener.onItemLongClick(v);
        return false;
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        private TextView text;
        private ImageView mImg;

        private MyHolder(View itemView, int viewType) {
            super(itemView);
            if (viewType == 0){
                text = ((TextView) itemView.findViewById(R.id.txt));
            }else {
                mImg = ((ImageView) itemView.findViewById(R.id.iv));
            }
        }
    }

    //点击监听
    public void setOnClickListener(OnItemClickListener listener){
        this.itemClickListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view);
        void onItemLongClick(View view);
    }

    //position监听
    public void setOnPositionListener(OnPositionListener listener){
        this.positionListener = listener;
    }

    public interface OnPositionListener{
        void onPosition(int position);
    }
}
